home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / updates / update21.zoo / lib / obstack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-03  |  11.0 KB  |  380 lines

  1. /* obstack.c - subroutines used implicitly by object stack macros
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <obstack.h>
  19. #include <compiler.h>
  20. #ifndef _SIZE_T
  21. #define _SIZE_T __SIZE_TYPEDEF__
  22. typedef _SIZE_T size_t;
  23. #endif
  24.  
  25. #ifdef __STDC__
  26. #define POINTER void *
  27. #else
  28. #define POINTER char *
  29. #endif
  30.  
  31. /* Determine default alignment.  */
  32. struct fooalign {char x; double d;};
  33. #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
  34. /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
  35.    But in fact it might be less smart and round addresses to as much as
  36.    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
  37. union fooround {long x; double d;};
  38. #define DEFAULT_ROUNDING (sizeof (union fooround))
  39.  
  40. /* When we copy a long block of data, this is the unit to do it with.
  41.    On some machines, copying successive ints does not work;
  42.    in such a case, redefine COPYING_UNIT to `long' (if that works)
  43.    or `char' as a last resort.  */
  44. #ifndef COPYING_UNIT
  45. #ifdef atarist
  46. #define COPYING_UNIT long    /* a long is ok because of the alignment above */
  47. #else
  48. #define COPYING_UNIT int
  49. #endif
  50. #endif
  51.  
  52. /* The non-GNU-C macros copy the obstack into this global variable
  53.    to avoid multiple evaluation.  */
  54.  
  55. struct obstack *_obstack;
  56.  
  57. /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
  58.    Objects start on multiples of ALIGNMENT (0 means use default).
  59.    CHUNKFUN is the function to use to allocate chunks,
  60.    and FREEFUN the function to free them.  */
  61.  
  62. void
  63. _obstack_begin (h, size, alignment, chunkfun, freefun)
  64.      struct obstack *h;
  65.      size_t size;
  66.      int alignment;
  67.      POINTER (*chunkfun) ();
  68.      void (*freefun) ();
  69. {
  70.   register struct _obstack_chunk* chunk; /* points to new chunk */
  71.  
  72.   if (alignment == 0)
  73.     alignment = DEFAULT_ALIGNMENT;
  74.   if (size == 0)
  75.     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
  76.     {
  77.       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
  78.      Use the values for range checking, because if range checking is off,
  79.      the extra bytes won't be missed terribly, but if range checking is on
  80.      and we used a larger request, a whole extra 4096 bytes would be
  81.      allocated.
  82.  
  83.      These number are irrelevant to the new GNU malloc.  I suspect it is
  84.      less sensitive to the size of the request.  */
  85.       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
  86.             + 4 + DEFAULT_ROUNDING - 1)
  87.            & ~(DEFAULT_ROUNDING - 1));
  88.       size = 4096 - extra;
  89.     }
  90.  
  91.   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
  92.   h->freefun = freefun;
  93.   h->chunk_size = size;
  94.   h->alignment_mask = alignment - 1;
  95.  
  96.   chunk    = h->chunk = (*h->chunkfun) (h->chunk_size);
  97.   h->next_free = h->object_base = chunk->contents;
  98.   h->chunk_limit = chunk->limit
  99.     = (char *) chunk + h->chunk_size;
  100.   chunk->prev = 0;
  101.   /* The initial chunk now contains no empty object.  */
  102.   h->maybe_empty_object = 0;
  103. }
  104.  
  105. /* Allocate a new current chunk for the obstack *H
  106.    on the assumption that LENGTH bytes need to be added
  107.    to the current object, or a new object of length LENGTH allocated.
  108.    Copies any partial object from the end of the old chunk
  109.    to the beginning of the new one.  */
  110.  
  111. void
  112. _obstack_newchunk (h, length)
  113.      struct obstack *h;
  114.      size_t length;
  115. {
  116.   register struct _obstack_chunk*    old_chunk = h->chunk;
  117.   register struct _obstack_chunk*    new_chunk;
  118.   register size_t    new_size;
  119.   register size_t obj_size = h->next_free - h->object_base;
  120.   register long i;
  121.   int already;
  122.  
  123.   /* Compute size for new chunk.  */
  124.   new_size = (obj_size + length) + (obj_size >> 3) + 100;
  125.   if (new_size < h->chunk_size)
  126.     new_size = h->chunk_size;
  127.  
  128.   /* Allocate and initialize the new chunk.  */
  129.   new_chunk = h->chunk = (*h->chunkfun) (new_size);
  130.   new_chunk->prev = old_chunk;
  131.   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
  132.  
  133.   /* Move the existing object to the new chunk.
  134.      Word at a time is fast and is safe if the object
  135.      is sufficiently aligned.  */
  136.   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
  137.     {
  138.       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
  139.        i >= 0; i--)
  140.     ((COPYING_UNIT *)new_chunk->contents)[i]
  141.       = ((COPYING_UNIT *)h->object_base)[i];
  142.       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
  143.      but that can cross a page boundary on a machine
  144.      which does not do strict alignment for COPYING_UNITS.  */
  145.       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
  146.     }
  147.   else
  148.     already = 0;
  149.   /* Copy remaining bytes one by one.  */
  150.   for (i = already; i < obj_size; i++)
  151.     new_chunk->contents[i] = h->object_base[i];
  152.  
  153.   /* If the object just copied was the only data in OLD_CHUNK,
  154.      free that chunk and remove it from the chain.
  155.      But not if that chunk might contain an empty object.  */
  156.   if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
  157.     {
  158.       new_chunk->prev = old_chunk->prev;
  159.       (*h->freefun) (old_chunk);
  160.     }
  161.  
  162.   h->object_base = new_chunk->contents;
  163.   h->next_free = h->object_base + obj_size;
  164.   /* The new chunk certainly contains no empty object yet.  */
  165.   h->maybe_empty_object = 0;
  166. }
  167.  
  168. /* Return nonzero if object OBJ has been allocated from obstack H.
  169.    This is here for debugging.
  170.    If you use it in a program, you are probably losing.  */
  171.  
  172. int
  173. _obstack_allocated_p (h, obj)
  174.      struct obstack *h;
  175.      POINTER obj;
  176. {
  177.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  178.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  179.  
  180.   lp = (h)->chunk;
  181.   /* We use >= rather than > since the object cannot be exactly at
  182.      the beginning of the chunk but might be an empty object exactly
  183.      at the end of an adjacent chunk. */
  184.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  185.     {
  186.       plp = lp->prev;
  187.       lp = plp;
  188.     }
  189.   return lp != 0;
  190. }
  191.  
  192. /* Free objects in obstack H, including OBJ and everything allocate
  193.    more recently than OBJ.  If OBJ is zero, free everything in H.  */
  194.  
  195. #undef obstack_free
  196.  
  197. /* This function has two names with identical definitions.
  198.    This is the first one, called from non-ANSI code.  */
  199.  
  200. void
  201. _obstack_free (h, obj)
  202.      struct obstack *h;
  203.      POINTER obj;
  204. {
  205.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  206.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  207.  
  208.   lp = h->chunk;
  209.   /* We use >= because there cannot be an object at the beginning of a chunk.
  210.      But there can be an empty object at that address
  211.      at the end of another chunk.  */
  212.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  213.     {
  214.       plp = lp->prev;
  215.       (*h->freefun) (lp);
  216.       lp = plp;
  217.       /* If we switch chunks, we can't tell whether the new current
  218.      chunk contains an empty object, so assume that it may.  */
  219.       h->maybe_empty_object = 1;
  220.     }
  221.   if (lp)
  222.     {
  223.       h->object_base = h->next_free = (char *)(obj);
  224.       h->chunk_limit = lp->limit;
  225.       h->chunk = lp;
  226.     }
  227.   else if (obj != 0)
  228.     /* obj is not in any of the chunks! */
  229.     abort ();
  230. }
  231.  
  232. /* This function is used from ANSI code.  */
  233.  
  234. void
  235. obstack_free (h, obj)
  236.      struct obstack *h;
  237.      POINTER obj;
  238. {
  239.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  240.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  241.  
  242.   lp = h->chunk;
  243.   /* We use >= because there cannot be an object at the beginning of a chunk.
  244.      But there can be an empty object at that address
  245.      at the end of another chunk.  */
  246.   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
  247.     {
  248.       plp = lp->prev;
  249.       (*h->freefun) (lp);
  250.       lp = plp;
  251.       /* If we switch chunks, we can't tell whether the new current
  252.      chunk contains an empty object, so assume that it may.  */
  253.       h->maybe_empty_object = 1;
  254.     }
  255.   if (lp)
  256.     {
  257.       h->object_base = h->next_free = (char *)(obj);
  258.       h->chunk_limit = lp->limit;
  259.       h->chunk = lp;
  260.     }
  261.   else if (obj != 0)
  262.     /* obj is not in any of the chunks! */
  263.     abort ();
  264. }
  265.  
  266. #if 0
  267. /* These are now turned off because the applications do not use it
  268.    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
  269.  
  270. /* Now define the functional versions of the obstack macros.
  271.    Define them to simply use the corresponding macros to do the job.  */
  272.  
  273. #ifdef __STDC__
  274. /* These function definitions do not work with non-ANSI preprocessors;
  275.    they won't pass through the macro names in parentheses.  */
  276.  
  277. /* The function names appear in parentheses in order to prevent
  278.    the macro-definitions of the names from being expanded there.  */
  279.  
  280. POINTER (obstack_base) (obstack)
  281.      struct obstack *obstack;
  282. {
  283.   return obstack_base (obstack);
  284. }
  285.  
  286. POINTER (obstack_next_free) (obstack)
  287.      struct obstack *obstack;
  288. {
  289.   return obstack_next_free (obstack);
  290. }
  291.  
  292. size_t (obstack_object_size) (obstack)
  293.      struct obstack *obstack;
  294. {
  295.   return obstack_object_size (obstack);
  296. }
  297.  
  298. size_t (obstack_room) (obstack)
  299.      struct obstack *obstack;
  300. {
  301.   return obstack_room (obstack);
  302. }
  303.  
  304. void (obstack_grow) (obstack, pointer, length)
  305.      struct obstack *obstack;
  306.      POINTER pointer;
  307.      size_t length;
  308. {
  309.   obstack_grow (obstack, pointer, length);
  310. }
  311.  
  312. void (obstack_grow0) (obstack, pointer, length)
  313.      struct obstack *obstack;
  314.      POINTER pointer;
  315.      size_t length;
  316. {
  317.   obstack_grow0 (obstack, pointer, length);
  318. }
  319.  
  320. void (obstack_1grow) (obstack, character)
  321.      struct obstack *obstack;
  322.      int character;
  323. {
  324.   obstack_1grow (obstack, character);
  325. }
  326.  
  327. void (obstack_blank) (obstack, length)
  328.      struct obstack *obstack;
  329.      size_t length;
  330. {
  331.   obstack_blank (obstack, length);
  332. }
  333.  
  334. void (obstack_1grow_fast) (obstack, character)
  335.      struct obstack *obstack;
  336.      int character;
  337. {
  338.   obstack_1grow_fast (obstack, character);
  339. }
  340.  
  341. void (obstack_blank_fast) (obstack, length)
  342.      struct obstack *obstack;
  343.      size_t length;
  344. {
  345.   obstack_blank_fast (obstack, length);
  346. }
  347.  
  348. POINTER (obstack_finish) (obstack)
  349.      struct obstack *obstack;
  350. {
  351.   return obstack_finish (obstack);
  352. }
  353.  
  354. POINTER (obstack_alloc) (obstack, length)
  355.      struct obstack *obstack;
  356.      size_t length;
  357. {
  358.   return obstack_alloc (obstack, length);
  359. }
  360.  
  361. POINTER (obstack_copy) (obstack, pointer, length)
  362.      struct obstack *obstack;
  363.      POINTER pointer;
  364.      size_t length;
  365. {
  366.   return obstack_copy (obstack, pointer, length);
  367. }
  368.  
  369. POINTER (obstack_copy0) (obstack, pointer, length)
  370.      struct obstack *obstack;
  371.      POINTER pointer;
  372.      size_t length;
  373. {
  374.   return obstack_copy0 (obstack, pointer, length);
  375. }
  376.  
  377. #endif /* __STDC__ */
  378.  
  379. #endif /* 0 */
  380.